(function($){
	$.fn.suggest=function(options){
		var settings = {
			'resultsClass': 'sg_results',
			'sLength' : 1, //关键词最低长度
			'matchClass' : 'red',
			'ajaxurl' : '' //Ajax地址
		};
		if(options){
			$.extend(settings, options);
		}
		
		this.each(function(){
			var $input=$(this);
			var $results = $(document.createElement("ul"));
			var InvalueO=''; //上次检索值
			var iPoint=-1; //提示的指针
			var cache = [];
			var ajax = ''; // ajax
			$results.addClass(settings.resultsClass);//.appendTo('body');
			$input.after($results);
			
			$input.attr("autocomplete", "off").on('blur',function() {
				setTimeout(function() {
					$results.hide()
				}, 200);
			}).on('keyup',function(e){
				var intKeyCode=e.keyCode,Invalue=$.trim($input.val());
				if(intKeyCode==13 || intKeyCode==9){
					selectCurrentResult();
					return false;	
				}				
				if ((/27$|38$|40$/.test(intKeyCode) && $results.is(':visible'))) {
					switch(intKeyCode) {	
						case 38: // up
							prevResult();
							break;				
						case 40: // down
							nextResult();
							break;
						case 27: //	escape
							$results.hide();
							break;
					}
				}else if (Invalue != InvalueO) {
					$input.attr('data-url', '');
					InvalueO = Invalue;
					suggest();
				}			
			}).on('keydown',function(e){
				var intKeyCode=e.keyCode;
				var Invalue=$.trim($input.val());
				// 回车 tab提交检索
				if(intKeyCode==9 || intKeyCode==13){
					$('#h_tao_searbtn').click();
					return false;
				}
			});
			
			// 
			function selResult(){
				$currentResult = getCurrentResult();
				if($currentResult){
					$input.val($currentResult.find('.lileft').text());
				}else{
					$input.val(InvalueO);
				}
			}
			
			// 鼠标按键方向下
			function nextResult() {
				$currentResult = getCurrentResult();
				if ($currentResult){
					$currentResult.removeClass(settings.selectClass).next().addClass(settings.selectClass);
				} else{
					$results.children('li:first-child').addClass(settings.selectClass);
				}
				selResult();
			}
			
			// 鼠标按键方向上
			function prevResult() {
				$currentResult = getCurrentResult();
				if ($currentResult){
					$currentResult.removeClass(settings.selectClass).prev().addClass(settings.selectClass);
				} else {
					$results.children('li:last-child').addClass(settings.selectClass);
				}
				selResult();
			}
			
			// 将选中值写入检索框
			function selectCurrentResult() {
				$currentResult = getCurrentResult();
				if ($currentResult) {
					$input.val($currentResult.find('.lileft').text());
					$results.hide();					
				}
				return false;
			}
			
			// 查询数据
			function suggest() {
				if ($('#js_h_taosearch .searchmenu a').index($('#js_h_taosearch .searchmenu a.select')) != 0){
					return false;
				}
				if (InvalueO.length>= settings.sLength) {
					cached = checkCache();
					if (cached) {
						displayItems(cached['items']);
					} else {
						if (ajax != ''){
							ajax.abort();
						}
						ajax = $.getJSON(settings.ajaxurl, {q:InvalueO}, function(data) {
							if(data.result==''){
								$results.hide();
								return false;
							}
							var items = data.result;
							displayItems(items);
							addToCache(InvalueO,items);
						},"json");
					}
				} else {
					$results.hide();
				}
			}
			
			// 提示框显示
			function displayItems(items) {
				var html = '';
				for (var i = 0; i < items.length; i++)
					html += '<li>' + '<span class="lileft">' + items[i][0] + '</span>' + '<span class="liright">共找到宝贝数量' + items[i][1] + '个</span>'
					 + '</li>';
				if(items.length>10){
					$results.addClass('fsuggest-scroll');	
				}
				$results.html(html).show();
				
				$results.find('li').on('mouseover', function(){$(this).addClass('hover');});
				$results.find('li').on('mouseout', function(){$(this).removeClass('hover');});
				$results.find('li').on('click',function(){selectCurrentResult();});
			}
			
			// 检查是否缓存
			function checkCache() {
				for (var i = 0; i < cache.length; i++){
					if (cache[i]['name'] ==InvalueO) {
						cache.unshift(cache.splice(i,1)[0]);
						return cache[0];
					}
				}				
				return false;			
			}
			
			// 添加缓存
			function addToCache(name, items) {
				cache.push({
					name: name,
					items: items
				});
			}

			// 当前结果
			function getCurrentResult() {
				if (!$results.is(':visible')){
					return false;
				}
				var $currentResult = $results.children('li.' + settings.selectClass);
				if (!$currentResult.length){
					$currentResult = false;
				}
				return $currentResult;
			}

		});	
	};
	
})(jQuery);